package com.briup.demo.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.briup.demo.bean.XcdOrder;
import com.briup.demo.mapper.XcdOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class XcdOrderService {

    @Autowired
    private XcdOrderMapper orderMapper;

    public List<XcdOrder> searchBy(
            Date begin,Date end,String key){
        return orderMapper.selectBy(begin, end, key);
    }

    public List<XcdOrder> getAllMember(){
        List<XcdOrder> list = orderMapper.selectAll();
        if(list!=null) {
            return list;
        }
        return new ArrayList<XcdOrder>();
    }

    public int deleteByOrderId(String id) {

        return orderMapper.deleteByPrimaryKey(id);
    }


    public int saveOrUpdate(String OrderId,String GoodsId,
                                String BuyerId,String BuyerName) {
        if(OrderId!=null&&!"".equals(OrderId)) {
           XcdOrder c = orderMapper.selectByPrimaryKey(OrderId);
            if(c==null) { 
                return -1;
            }
        }
        if(OrderId==null||"".equals(OrderId)) {
            XcdOrder ord = new XcdOrder();
            ord.setOrderid(UUID.randomUUID().toString());
            ord.setGoodsid(GoodsId);
            ord.setBuyername(BuyerName);
            ord.setBuyerid(BuyerId);
            ord.setOrderdate(new Date());
            orderMapper.insert(ord);
            return 1;
        }else {
            XcdOrder ord =
                    orderMapper.selectByPrimaryKey(OrderId);
            if(ord==null) {
                return -1;
            }
            ord.setGoodsid(GoodsId);
            ord.setBuyername(BuyerName);
            ord.setBuyerid(BuyerId);
            orderMapper.updateByPrimaryKey(ord);
        }
        return 0;
    }

}

